package one.stand.controller;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import com.jmp.api.admin.dto.DynamicRemoveRequest;
import com.jmp.api.admin.dto.DynamicRequest;
import com.jmp.api.admin.dto.DynamicResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.DynamicListDto;
import com.jmp.base.dto.DynamicListVo;
import com.jmp.base.dto.DynamicRemoveDto;
import com.jmp.feign.DynamicFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.Company2Dto;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.dto.UserDto;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import one.stand.service.RecordPublicService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api(description = "红木圈管理")
@RestController
@RequestMapping("adminApi/dynamic")
public class DynamicController {
    @Autowired
    private DynamicFeign dynamicFeign;

    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private RecordPublicService recordPublicService;

    @ApiOperation(value = "1列表")
    @GetMapping("/list")
    @SecurityAuthCheck
    public ResultModel<DynamicResponse> list(DynamicRequest request) {
        DynamicListDto dynamicListDto = DynamicListDto.builder()
                .keyword(request.getKeyword())
                .build();
        dynamicListDto.setPageNum(request.getPageNum());
        dynamicListDto.setPageSize(request.getPageSize());
        ResultModel<DynamicListVo> resultModel = dynamicFeign.list(dynamicListDto);
        AssertUtil.isTrue(resultModel);

        PageResponse2<DynamicResponse.Dynamic> dynamicPageResponse2 = ConvertUtils.page2(resultModel.getData().getDynamics(), dynamic -> {

            UserDto user = userAdminService.get(dynamic.getCreateUserId());
            Company2Dto company = companyBaseService.get2(dynamic.getCompanyId());
            BusinessCard card = businessCardBaseService.getByUserId(dynamic.getCreateUserId());

            return DynamicResponse.Dynamic.builder()
                    .dynamicId(dynamic.getDynamicId())
                    .time(dynamic.getTime())
                    .type(dynamic.getType())
                    .title(dynamic.getTitle())
                    .photos(StrUtil.split(dynamic.getPhotos(), CharUtil.COMMA))

                    .companyId(company.getCompanyId())
                    .companyName(company.getCompanyName())
                    .companyLogo(company.getCompanyLogo())

                    .userId(user.getUserId())
                    .userName(user.getName())
                    .userImg(user.getUserImg())
                    .cardId(Objects.nonNull(card) ? card.getCardId() : null)

                    .commentNum(recordPublicService.countCommentByDynamic(dynamic.getDynamicId()))
                    .doThumbsNum(recordPublicService.getThumbsCount(RecordPublicService.THUMBS_TYPE_ARTICLE, dynamic.getDynamicId()))

                    .build();
        });

        DynamicResponse response = DynamicResponse.builder()
                .dynamics(dynamicPageResponse2)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "2删除")
    @PostMapping("/remove")
    @SecurityAuthCheck
    public ResultModel remove(@RequestBody DynamicRemoveRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        ResultModel resultModel = dynamicFeign.remove(DynamicRemoveDto.builder()
                .dynamicId(request.getDynamicId())
                .optUserId(optUser.getUserId())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }
}
